package top.hmtools.mapperUnit;

import org.springframework.stereotype.Component;

import top.hmtools.base.StringTools;
import top.hmtools.beans.ColumnsInfoBean;
import top.hmtools.common.ECurdType;
import top.hmtools.system.SystemInfoTools;

/**
 * 批量物理删除sql语句片段
 * @author Pucou
 *
 */
@Component
public class DelRealByIdsMapperUnit extends AMapperUnitBase{

	public ECurdType getCurdType() {
		return ECurdType.DELATE;
	}

	public String createContent(String basePackage, String methodName,ColumnsInfoBean... beans) {
		StringBuffer result = new StringBuffer();
		String tableAliseName = "aa";
		String tableName = beans[0].getTable_name();
        if(StringTools.isBlank(methodName)){
            methodName = "delRealByIds";
        }
        
        String priKeyColumnName = null;
        for(ColumnsInfoBean bean:beans){
        	if("PRI".equals(bean.getColumn_key().trim())){
        		priKeyColumnName = bean.getColumn_name();
        		break;
        	}
        }
        
        result.append("<delete id=\""+methodName+"\">");
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append(" DELETE ");
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append(tableAliseName+".* ");
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append(" FROM "+tableName+" AS "+tableAliseName);
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append(" WHERE ");
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append(tableAliseName+"."+priKeyColumnName+" IN ");
        result.append(SystemInfoTools.getOSLineSeparator());
        
        result.append("(");
        result.append(SystemInfoTools.getOSLineSeparator());

        result.append("<foreach collection=\"ids\" item=\"item\" index=\"index\" separator=\",\" >");
        result.append(SystemInfoTools.getOSLineSeparator());

        result.append("#{item}");

        result.append(SystemInfoTools.getOSLineSeparator());
        result.append("</foreach>");
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append(")");
        result.append(SystemInfoTools.getOSLineSeparator());
        
        
        result.append("</delete>");
        
        result.append("");
        result.append("");
        result.append("");
        result.append("");
        result.append("");
        
        return result.toString();
	}

}
